using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Web;
using System.Net;
using System.Net.Sockets;

namespace LowLevelGraphics.ImageAcquiring
{
    /// <summary>
    /// 
    /// </summary>
	public enum SearchType
	{
        /// <summary>
        /// 
        /// </summary>
		huge,
        /// <summary>
        /// 
        /// </summary>
		xxlarge,
        /// <summary>
        /// 
        /// </summary>
		icon,
        /// <summary>
        /// 
        /// </summary>
		small,
        /// <summary>
        /// 
        /// </summary>
		all
	}

    /// <summary>
    /// 
    /// </summary>
	public enum ImageType
	{
        /// <summary>
        /// 
        /// </summary>
		news,
        /// <summary>
        /// 
        /// </summary>
		face,
        /// <summary>
        /// 
        /// </summary>
		clipart,
        /// <summary>
        /// 
        /// </summary>
		lineart,
        /// <summary>
        /// 
        /// </summary>
		photo,
        /// <summary>
        /// 
        /// </summary>
		all
	}

    /// <summary>
    /// 
    /// </summary>
	public class GoogleOnlineSearchProvider	: OnlineSearchProvider
	{
		public event EventHandler FileDownloaded;

		public Dictionary<SearchType, string> aSearchType = new Dictionary<SearchType, string>();
		public Dictionary<ImageType, string> aImageType = new Dictionary<ImageType, string>();

		private ImageType m_ImageType = ImageType.all;
		private SearchType m_SearchType = SearchType.all;

        /// <summary>
        /// Initializes a new instance of the <see cref="GoogleOnlineSearchProvider"/> class.
        /// </summary>
		public GoogleOnlineSearchProvider()
		{
			this.Host = "images.google.de";

			aSearchType.Add(SearchType.huge,"huge");   //gross
			aSearchType.Add(SearchType.xxlarge,"xxlarge"); //extragross
			aSearchType.Add(SearchType.small,"small|medium|large|xlarge"); //mittlere bilder
			aSearchType.Add(SearchType.icon, "icon");		//kleine bilder
			aSearchType.Add(SearchType.all,"");

			aImageType.Add(ImageType.clipart, "clipart");
			aImageType.Add(ImageType.face, "face");
			aImageType.Add(ImageType.lineart, "lineart");
			aImageType.Add(ImageType.news, "news");
			aImageType.Add(ImageType.photo, "photo");
			aImageType.Add(ImageType.all,"");
		}

        /// <summary>
        /// Gets the google string.
        /// </summary>
        /// <param name="_sSearch">The _s search.</param>
        /// <returns></returns>
		public string GetGoogleString(string _sSearch)
		{
			string[] aElem = _sSearch.Split(' ');
			return string.Join("+", aElem);
		}

        /// <summary>
        /// Downloads the files.
        /// </summary>
        /// <param name="_sSearch">The _s search.</param>
		public override void DownloadFiles(object _sSearch)
		{
			DownloadGooglePics((string)_sSearch, true);
		}

        /// <summary>
        /// Downloads the google pics.
        /// </summary>
        /// <param name="_sSearch">The _s search.</param>
        /// <param name="_bUseWebClient">if set to <c>true</c> [_b use web client].</param>
		public void DownloadGooglePics(string _sSearch, bool _bUseWebClient)
		{
			string sText = string.Empty;

			if (_bUseWebClient)
			{
				WebClient client = new WebClient();
				string sDownloadString = "http://"+this.Host+"/images?hl=de&q=" + _sSearch + "&btnG=Bilder-Suche&gbv=2hl=de&sa=G&imgsz="+aSearchType[m_SearchType];
				sText = client.DownloadString(sDownloadString);
			}
			else
			{
				sText = GetSocketData(this.Host, "/images?um=1&hl=de&client=firefox-a&rls=org.mozilla%3Ade%3Aofficial&q="+_sSearch+"&btnG=Bilder-Suche", this.Host);
			}

			int nX = 0;
			int nY = 0;string[] saLink = Regex.Split(sText, ";dyn.Img");
			for (int i = 1; i < saLink.Length; i++)
			{
				try
				{
					saLink[i] = saLink[i].Replace("/imgres?imgurl=", "");
					string[] saPic = Regex.Split(saLink[i], "&imgrefurl=");
					saPic[0] = saPic[0].Replace("(", "").Replace("\"", "");

					Console.WriteLine("Pic " + i.ToString() + ": " + saPic[0]);
					HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(saPic[0]);
					HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

					Stream stream = webResponse.GetResponseStream();
					if (FileDownloaded != null)
					{
						FileDownloaded(stream, new EventArgs());
					}
				}
				catch (Exception ex)
				{
					Console.WriteLine(ex.ToString());
				}
			}
		}

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
		public override string ToString()
		{
			return "Google";
		}
	}
}
